package com.bojiu.lawsearch.lawsearch.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @Author afdiao
 * @Date 2020/2/26 0:34
 */
@Slf4j
public class UtilTools {
    /**
     * 创建查询条件
     *
     * @param key
     * @param value
     * @param type
     * @return
     */
    public static Example createExampleByKey(String key, Object value, Class type) {
        Example example = new Example(type);
        example.createCriteria().andEqualTo(key, value);
        return example;
    }

    public static Example createExampleByMap(Map<String, Object> map, Class type) {
        Example example = new Example(type);
        Example.Criteria criteria = example.createCriteria();
        map.keySet().stream().forEach(key -> {
            criteria.andEqualTo(key, map.get(key));
        });
        return example;
    }

    /**
     * 创建uuid
     *
     * @return
     */
    public static String createUUid() {
        String string = UUID.randomUUID().toString();
        String replaceAll = string.replaceAll("-", "");
        return replaceAll;
    }

    /**
     * 处理字段为空
     *
     * @param errors
     * @return
     */
    public static JsonResult handleNullField(Errors errors) {
        String defaultMessage = errors.getFieldError().getDefaultMessage();
        log.error("field is null :{}", defaultMessage);
        return new JsonResult(ResultCode.NULLFIELD, defaultMessage);
    }

    /**
     * 逗号分隔转sql
     *
     * @param string
     * @return
     */
    public static String changeStrForSql(String string) {
        if (StringUtils.isEmpty(string)) {
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (String str : string.split(",")) {
            stringBuffer.append("'").append(str).append("',");
        }
        String substring = stringBuffer.substring(0, stringBuffer.length() - 1);
        return "(" + substring + ")";
    }


    /**
     * list转sql
     *
     * @param ids
     * @return
     */
    public static String changeListForSql(List<Integer> ids) {
        if (null == ids || ids.isEmpty()) {
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (Integer id : ids) {
            stringBuffer.append("'").append(id).append("',");
        }
        String substring = stringBuffer.substring(0, stringBuffer.length() - 1);
        return "(" + substring + ")";
    }

    /**
     * set转sql
     *
     * @param setId
     * @return
     */
    public static String changeSetForSql(Set<Integer> setId) {
        if (null == setId || setId.isEmpty()) {
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (Integer id : setId) {
            stringBuffer.append("'").append(id).append("',");
        }
        String substring = stringBuffer.substring(0, stringBuffer.length() - 1);
        return "(" + substring + ")";
    }
}
